<template>
	<sc-dialog v-model="visible" title="创建返工单" show-fullscreen destroy-on-close width="750px" @close="close">
		<el-form ref="formRef" label-width="140px" :model="formData" :rules="rules">
			<el-form-item label="返工单号" prop="doc">
				<el-input v-model.trim="formData.doc" disabled />
			</el-form-item>
			<el-form-item label="SN" prop="sn">
				<el-input v-model.trim="formData.sn" disabled />
			</el-form-item>
			<el-form-item label="NG 工序编号" prop="ngProcCode">
				<el-input v-model.trim="formData.ngProcCode" disabled />
			</el-form-item>
			<el-form-item label="返工类型" prop="reworkType">
				<el-radio-group v-model="formData.reworkType">
					<el-radio :label="1">离线返工</el-radio>
					<el-radio :label="2">在线返工</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item v-if="formData.reworkType === 1" label="返工模板" prop="templateId">
				<el-select v-model="formData.templateId" placeholder="请选择返工模板">
					<el-option
						v-for="item in reworkTemplateOptions"
						:key="item.id"
						:label="item.templateName"
						:value="item.id" />
				</el-select>
			</el-form-item>
			<el-form-item v-if="formData.reworkType === 1" label="是否需要重新上线" prop="needRelaunch">
				<el-col :span="3">
					<el-switch v-model="formData.needRelaunch" inline-prompt active-text="是" inactive-text="否" />
				</el-col>
				<el-col :span="21">
					<el-alert type="warning" show-icon :closable="false" style="height: 30px;">
						若选择 “否”，返工结束后产品将变更 “完工下线” 状态。
					</el-alert>
				</el-col>
			</el-form-item>
			<el-form-item
				v-if="!(formData.reworkType === 1 && !formData.needRelaunch)"
				label="重上线作业工序"
				prop="relaunchProcId">
				<el-col :span="10">
					<el-select v-model="formData.relaunchProcId" placeholder="请选择重上线作业工序">
						<el-option
							v-for="item in reworkProcOptions"
							:key="item.procId"
							:label="item.procCode"
							:value="item.procId"
							:disabled="item.disabled" />
					</el-select>
				</el-col>
				<el-col :span="14">
					<el-button round type="primary" size="small" @click="prevProc">前一工序</el-button>
					<el-button round type="primary" size="small" @click="nextProc">后一工序</el-button>
				</el-col>
			</el-form-item>
			<el-form-item label="作业开始步序" prop="relaunchProcStep">
				<el-input-number v-model="formData.relaunchProcStep" :min="0" :max="999" disabled />
			</el-form-item>
		</el-form>

		<template #footer>
			<el-button @click="close">取 消</el-button>
			<el-button :loading="isSaveing" type="primary" @click="save">确 定</el-button>
		</template>
	</sc-dialog>
</template>

<script>
export default {
    emits: ['complete'],
    data() {
        return {
            isSaveing: false,
            visible: false,
            reworkTemplateOptions: [],
            reworkProcOptions: [],
            formData: {
                id: 0,
                doc: '',
                sn: '',
                ngProcCode: '',
                reworkType: 1, // 1->离线返工;2->在线返工
                needRelaunch: true,
                relaunchProcId: undefined,
                relaunchProcCode: '',
                relaunchProcStep: 0,
                templateId: undefined,
                templateName: '',
            },
            rules: {
                doc: [{ required: true, message: "请输入返工单号", trigger: "blur" }],
                sn: [{ required: true, message: "请输入返修 SN", trigger: "blur" }],
                reworkType: [{ required: true, message: "请选择返修类型", trigger: "blur" }],
                relaunchProcId: [{ required: true, message: "请选择重上线作业工序", trigger: "blur" }],
                templateId: [{ required: true, message: "请选择返工模板", trigger: "blur" }],
            }
        };
    },
    methods: {
        async open(row) {
            const resp = await this.$API.prodtranssn.model.get(row.id);
            const data = resp.data;

            const resp0 = await this.$API.common.datetimeFormat.get(); // 取服务器时间
            const data0 = resp0.data;

            this.formData.doc = `RW${data0}`;
            this.formData.sn = data.sn;
            this.formData.ngProcCode = data.curProcCode;

            const resp1 = await this.$API.reworktemplate.list.get();
            this.reworkTemplateOptions = resp1.data;
            const defaultTemplate = resp1.data.find(s => s.isDefault);
            if (defaultTemplate) {
                this.formData.templateId = defaultTemplate.id;
                this.formData.templateName = defaultTemplate.templateName;
            }

            const resp2 = await this.$API.reworkdoc.proclist.get({ routingId: row.curRoutingId, procId: row.curProcId });
            this.reworkProcOptions = resp2.data;
            const selectedReworkProc = this.reworkProcOptions.find(s => s.selected);
            this.formData.relaunchProcId = selectedReworkProc.procId;
            this.formData.relaunchProcCode = selectedReworkProc.procCode;

            this.visible = true;
        },
        save() {
            this.$refs.formRef.validate(async (valid) => {
                if (valid) {
                    this.isSaveing = true;

                    let resp = null;
                    if (this.formData.id === 0) {
                        resp = await this.$API.reworkdoc.add.post(this.formData);
                    } else {
                        resp = await this.$API.reworkdoc.update.put(this.formData);
                    }
                    this.isSaveing = false;
                    if (resp.code == 200) {
                        this.$emit("complete");
                        this.visible = false;
                        this.$message.success("保存成功");
                    } else {
                        this.$alert(resp.message, "提示", { type: "error" });
                    }
                }
            });
        },
        prevProc() {
            if (this.reworkProcOptions.length > 1) {
                for (let i = 1; i < this.reworkProcOptions.length; i++) {
                    const item = this.reworkProcOptions[i];
                    if (item.procId === this.formData.relaunchProcId) {
                        const prev = this.reworkProcOptions[i - 1];
                        this.formData.relaunchProcId = prev.procId;
                        this.formData.relaunchProcCode = prev.procCode;
                        break;
                    }
                }
            }
        },
        nextProc() {
            if (this.reworkProcOptions.length > 1) {
                for (let i = 0; i < this.reworkProcOptions.length - 1; i++) {
                    const item = this.reworkProcOptions[i];
                    if (item.procId === this.formData.relaunchProcId) {
                        const next = this.reworkProcOptions[i + 1];
                        this.formData.relaunchProcId = next.procId;
                        this.formData.relaunchProcCode = next.procCode;
                        break;
                    }
                }
            }
        },
        close() {
            this.formData = {
                id: 0,
                doc: '',
                sn: '',
                ngProcCode: '',
                reworkType: 1, // 1->离线返工;2->在线返工
                needRelaunch: true,
                relaunchProcId: undefined,
                relaunchProcCode: '',
                relaunchProcStep: 0,
                templateId: undefined,
                templateName: '',
            };
            this.$refs.formRef.resetFields();
            this.visible = false;
        }
    }
}
</script>
